package xyz.labmem.app

import cn.hutool.core.io.file.FileReader
import it.sauronsoftware.junique.AlreadyLockedException
import it.sauronsoftware.junique.JUnique
import javafx.application.Application
import xyz.labmem.app.main.MainApplication
import xyz.labmem.lipx.core.context.Secret
import xyz.labmem.lipx.core.tool.LogInfo
import xyz.labmem.lipx.core.tool.getResourceFile
import kotlin.system.exitProcess

class LipxAppMain
fun main(args: Array<String>) {
    var secretUrl: String? = null
    args.forEach {
        if (it.startsWith("--secret:")) {
            secretUrl = it.substringAfter("--secret:")
        }
        if (it.startsWith("--debug"))
            LogInfo.debug = true
    }
    if (secretUrl != null) {
        Secret(FileReader(secretUrl).readString())
    } else
        Secret(getResourceFile("secret.key").readUtf8Str())
    junique()
    Application.launch(MainApplication::class.java, *args)
}

private fun junique() {
    try {
        JUnique.acquireLock(LipxAppMain::class.java.name)
    } catch (e: AlreadyLockedException) {
        println("已经启动Lipx App啦")
        exitProcess(0)
    }
}